package org.example.graalvmdemo.service.impl;

import org.example.graalvmdemo.model.User;
import org.example.graalvmdemo.service.TestService;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

@Service
public class TestServiceImpl implements TestService {
    @Override
    public String getUserById(Long id) {
        String name = "";
        try {
            Method getTestName = User.class.getMethod("getTestName");

            name = (String) getTestName.invoke(User.class.newInstance());
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
        // 模拟数据库查询
        return name;
    }

    @Override
    public String getWelcomeMessage() {
        // 读取资源文件
        Properties props = new Properties();
        try (InputStream is = getClass().getResourceAsStream("/static/messages.properties")) {
            props.load(is);
            return props.getProperty("welcome", "Default Welcome");
        } catch (IOException e) {
            return "Error loading message";
        }
    }
}
